package com.swu.pad.comment.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swu.pad.comment.entity.Comment;
import com.swu.pad.comment.entity.CommentQuestion;
import com.swu.pad.comment.entity.CommentTemplate;
import com.swu.pad.comment.service.CommentService;
import com.swu.pad.comment.service.CommentTemplateService;
import com.swu.pad.system.entity.User;

@Controller
@RequestMapping(value = "/comment")
public class CommentController {

	@Autowired
	private CommentTemplateService commentTemplateService;
	@Autowired
	private CommentService commentService;

	@ResponseBody
	@RequestMapping(value = "/getComments")
	public List<Comment> getComments(String assignmentPadId) {
		return commentService.getComment(assignmentPadId);
	}

	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateComment")
	public String saveOrUpdateComment(Comment comment) {
		commentService.saveOrUpateComment(comment);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteComment")
	public String deleteComment(String commentId) {
		commentService.deleteComment(commentId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getTemplates")
	public List<CommentTemplate> getTemplates() {
		return commentTemplateService.getTemplates();
	}

	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateTemplate")
	public String saveOrUpdateTemplate(CommentTemplate commentTemplate) {
		commentTemplateService.saveOrUpateTemplate(commentTemplate);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteTemplate")
	public String deleteTemplate(String templateId) {
		commentTemplateService.deleteTemplate(templateId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getTemplateQuestions")
	public List<CommentQuestion> getTemplateQuestions(String templateId) {
		return commentTemplateService.getTemplateQuestions(templateId);
	}

	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateQuestion")
	public String saveOrUpdateQuestion(CommentQuestion commentQuestion) {
		commentTemplateService.saveOrUpateQuestion(commentQuestion);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteTemplateQuestion")
	public String deleteTemplateQuestion(String questionId) {
		commentTemplateService.deleteQuestion(questionId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getCommentQuestions")
	public Map<String, Object> getCommentQuestions(String assignmentId, HttpSession session) {
		User user = (User) session.getAttribute("user");
		return commentTemplateService.getCommentQuestions(user.getId(), assignmentId);
	}
}
